package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.api.mapper.Gwaq554Mapper;
import cn.e4j.modular.api.service.Gwaq554Service;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yuhuan on 2018/08/15.
 */
@RestController
@RequestMapping("api/wx/gwaq554")
public class Gwaq554Controller extends ControllerSupport {

    private static Logger logger = LoggerFactory.getLogger(Gwaq554Controller.class);

    @Autowired
    Gwaq554Service gwaq554Service;

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    GwbService gwbService;

    /**
     * 获取本年成单信息列表
     *
     * @return
     */
    @RequestMapping(value = "getGwaq554List")
    @ResponseBody
    public ResponseData getGwaq554List(@DtoParam DtoWapper wapper) {
        Dto inDto =wapper.getDto();
        SysUser currentUser = wapper.getSysUser();

        inDto.put("GWCSENT", inDto.getString("ENT"));
        //inDto.put("gwbhacti", "Y");
        inDto.put("ID_", currentUser.getID());
        inDto.put("LANG", LangUtils.getLang(currentUser.getLANG()));
        inDto.put("YEAR", DateUtils.formatDate(new Date(), "yyyy"));

        //客户权限
        Dto gwb001 = gwbService.getGwb001(currentUser,inDto);
        inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

        //上下级权限
        Dto sys003 = systemService.getSys0031(currentUser);
        inDto.put("ORGS", sys003.get("ORGS"));

        //业务员权限
        Dto sys004 = systemService.getSys004(currentUser,(List<Dto>) sys003.get("ORGS"));
        inDto.put("YWY", sys004.get("YWY"));
        return new SuccessResponseData(gwaq554Service.getGwaq554ListPage(inDto));
    }
//
    /**
     * 获取本年商机信息列表
     *
     * @return
     */
    @RequestMapping(value = "getGwaq555List")
    @ResponseBody
    public ResponseData getGwaq555List(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();

        inDto.put("GWBHENT", inDto.getString("ENT"));
        inDto.put("ID_", currentUser.getID());
        inDto.put("LANG", LangUtils.getLang(currentUser.getLANG()));
        inDto.put("YEAR", DateUtils.formatDate(new Date(), "yyyy"));

        //客户权限
//            Dto gwb001 = gwbService.getGwb001(inDto);
//            inDto.put("customers", gwb001.get("customers"));

        //上下级权限
        Dto sys003 = systemService.getSys0031(currentUser);
        inDto.put("ORGS", sys003.get("ORGS"));

        //业务员权限
        Dto sys004 = systemService.getSys004(currentUser,(List<Dto>) sys003.get("ORGS"));
        inDto.put("YWY", sys004.get("YWY"));

        return new SuccessResponseData(InvokeUtil.invokeBeanMethod("gwaq554Mapper,getGwaq555ListPage",inDto));
    }
//
    /**
     * 获取本年商机信息
     *
     * @return
     */
    @RequestMapping(value = "getGwaq555Count")
    @ResponseBody
    public ResponseData getGwaq555Count(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = Dtos.newDto();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWBHENT", inDto.getString("ENT"));
            inDto.put("ID_", currentUser.getID());
            inDto.put("LANG", LangUtils.getLang(currentUser.getLANG()));
            inDto.put("YEAR", DateUtils.formatDate(new Date(), "yyyy"));

            //上下级权限
            Dto sys003 = systemService.getSys0031(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(currentUser,(List<Dto>) sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));
            inDto.put("GWBH031", "A");
            outDto.put("count1", gwaq554Service.getGwaq555List(inDto).getString("GWBK005"));
            inDto.put("GWBH031", "B");
            outDto.put("count2", gwaq554Service.getGwaq555List(inDto).getString("GWBK005"));
            inDto.put("GWBH031", "C");
            outDto.put("count3", gwaq554Service.getGwaq555List(inDto).getString("GWBK005"));
            inDto.put("GWBH031", "D");
            outDto.put("count4", gwaq554Service.getGwaq555List(inDto).getString("GWBK005"));
        } catch (Exception e) {
            e.printStackTrace();
            outDto.setAppCode(500);
        }
        return new SuccessResponseData(outDto);
    }
//
    /**
     * 获取本年拜访信息
     * @return
     */
    @RequestMapping(value = "getGwaq556Count")
    @ResponseBody
    public ResponseData getGwaq556Count(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = Dtos.newDto();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWBHENT", inDto.getString("ENT"));
            inDto.put("ID", currentUser.getID());
            inDto.put("LANG", LangUtils.getLang(currentUser.getLANG()));
            inDto.put("YEAR", DateUtils.formatDate(new Date(), "yyyy"));
            inDto.put("MONTH", DateUtils.formatDate(new Date(), "MM"));
            inDto.put("GWAK006", "1");

            //上下级权限
            Dto sys003 = systemService.getSys0031(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(currentUser,(List<Dto>) sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));

            outDto.put("count1", ((Dto)InvokeUtil.invokeBeanMethod("gwaq554Mapper,getGwaq556Count",inDto)).getString("count"));
            //客户权限
            inDto.put("GWAK006", "1");
            Dto gwb001 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list = (List<Dto>) gwb001.get("CUSTOMERS");
            inDto.put("CUSTOMERS", _list);

            //经销商权限
            inDto.put("GWAK006", "2");
            Dto gwb001_1 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list1 = (List<Dto>) gwb001_1.get("CUSTOMERS");
            inDto.put("DISTRIBUTORS", _list1);
            outDto.put("count2", ((Dto)InvokeUtil.invokeBeanMethod("gwaq554Mapper,getGwaq556Count2",inDto)).getString("count"));
        } catch (Exception e) {
            e.printStackTrace();
            outDto.setAppCode(500);
        }
        return new SuccessResponseData(outDto);
    }
//
//
    /**
     * 获取本月拜访信息列表
     *
     * @return
     */
    @RequestMapping(value = "getGwaq556List")
    @ResponseBody
    public ResponseData getGwaq556List(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();

        inDto.put("GWBHENT", inDto.getString("ENT"));
        //inDto.put("gwbhacti", "Y");
        inDto.put("ID", currentUser.getID());
        inDto.put("LANG", LangUtils.getLang(currentUser.getLANG()));
        inDto.put("YEAR", DateUtils.formatDate(new Date(), "yyyy"));
        inDto.put("MONTH", DateUtils.formatDate(new Date(), "MM"));

        //上下级权限
        Dto sys003 = systemService.getSys0031(currentUser);
        inDto.put("ORGS", sys003.get("ORGS"));
        List<Dto> result = null;
        //业务员权限
        Dto sys004 = systemService.getSys004(currentUser,(List<Dto>) sys003.get("ORGS"));
        inDto.put("YWY", sys004.get("YWY"));
        if (inDto.getString("GWAK006").equals("1")) {
            result = gwaq554Service.getGwaq556ListPage(wapper.getPage(), inDto);

        } else if (inDto.getString("GWAK006").equals("2")) {
            //客户权限
            inDto.put("GWAK006", "1");
            Dto gwb001 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list = (List<Dto>) gwb001.get("customers");
            inDto.put("CUSTOMERS", _list);

            //经销商权限
            inDto.put("GWAK006", "2");
            Dto gwb001_1 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list1 = (List<Dto>) gwb001_1.get("CUSTOMERS");
            inDto.put("DISTRIBUTORS", _list1);
            result = gwaq554Service.getGwaq556ListPage(wapper.getPage(), inDto);
        }

        return new SuccessResponseData(result);
    }


    /**
     * 根据成交率分类（A\B\C）统计商机明细数量
     *
     * @return
     * @author wenchuan.li on 2018/11/21 17:40
     */
    @RequestMapping(value = "statsSJ")
    @ResponseBody
    public ResponseData statsSJ(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = Dtos.newDto();
        SysUser user = wapper.getSysUser();
        try {

            inDto.put("GWCSENT",inDto.getString("ENT"));
            inDto.put("GWBHENT",inDto.getString("ENT"));
            inDto.put("ID", user.getID());
            inDto.put("LANG", CommonUtils.getLang(user.getLANG()));
            inDto.put("YEAR", DateUtils.formatDate(new Date(), "yyyy"));
            inDto.put("MONTH", DateUtils.formatDate(new Date(), "MM"));

            //客户权限
            Dto gwb001 = gwbService.getGwb001(user,inDto);
            inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

            //上下级权限
            Dto sys003 = systemService.getSys0031(user);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(user,(List<Dto>) sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));

            outDto.put("GWAQ554", gwaq554Service.getGwaq554List(inDto));
            outDto.put("GWAQ555", gwaq554Service.getGwaq555List(inDto));
            String count1 = gwaq554Service.getGwaq556Count(inDto).getString("COUNT");
            inDto.put("GWAK006", "1");
            Dto gwb001_2 = gwbService.getGwb001(user,inDto);
            List<Dto> _list = (List<Dto>) gwb001_2.get("CUSTOMERS");
            inDto.put("CUSTOMERS", _list);

            //经销商权限
            inDto.put("GWAK006", "2");
            Dto gwb001_1 = gwbService.getGwb001(user,inDto);
            List<Dto> _list1 = (List<Dto>) gwb001_1.get("CUSTOMERS");
            inDto.put("DISTRIBUTORS", _list1);
            String count2 = gwaq554Service.getGwaq556Count2(inDto).getString("COUNT");
            int count = Integer.parseInt(count1) + Integer.parseInt(count2);

            outDto.put("GWAQ556", count);
        } catch (Exception e) {
            e.printStackTrace();
            outDto.setAppCode(500);
        }
        return new SuccessResponseData(outDto);
    }


}
